Skip to content

ENH: Add use_nullable_dtypes in csv internals #48403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

phofl
Copy link
Member

@phofl phofl commented Sep 5, 2022

This adds the required casting logic for extension array dtypes. Want to get this done as a first step before adding a public option to read_csv. The unit tests can be used as a guideline for the required behaviour.

This always casts to ea dtypes, even when no missing values are present. From a users perspective this makes the most sense imo. You don't want to do a follow up operation and end up with float in a pipeline, when you set nullable dtypes (reindexing, enlargement in indexing ops, ...). That said, if there are concerns about this, we could also do something like

use_nullable_dtypes=False | always | when necessary

cc @jorisvandenbossche

@phofl phofl added IO CSV read_csv, to_csv NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Sep 5, 2022
@@ -15,6 +15,13 @@ import warnings

from pandas.util._exceptions import find_stack_level

from pandas import StringDtype
from pandas.core.arrays import (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a viable way to do this outside of the cython code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible option might be to change _maybe_upcast to optionally return a values array + mask array, and then do the actual construction in python?

Although since _maybe_upcast is only being called in this file (and thus from cython), that won't help. Unless if we would propagate such a (values, mask) tuple (instead of ArrayLike) through the different calls and return that from the TextReader.read method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah not worth contorting ourselves to avoid non-cython imports. just if there's a convenient alternative

Copy link
Member Author

@phofl phofl Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be done yes, but would make logic more complex, which is something we should avoid here I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, never mind then. thanks for taking a look

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mroeschke mroeschke added this to the 1.6 milestone Sep 13, 2022
@mroeschke mroeschke merged commit 1273bc9 into pandas-dev:main Sep 19, 2022
@mroeschke
Copy link
Member

Thanks @phofl

@phofl phofl deleted the enh_add_use_nullable_dtypes_in_maybe_upcast branch September 20, 2022 08:23
@mroeschke mroeschke modified the milestones: 1.6, 2.0 Oct 13, 2022
noatamir pushed a commit to noatamir/pandas that referenced this pull request Nov 9, 2022
* ENH: Add use_nullable_dtypes in csv internals

* Add tests

* Fix mypy

* Add comment

* Add pyarrow test

* Fix float32

* Fix float32

* Add contextmanager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO CSV read_csv, to_csv NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants